#include "LDAP_Operation.h"


LDAP_Operation::LDAP_Operation(void)
{
	result=0;
	DN=0;
}

LDAP_Operation::LDAP_Operation(char *dn)
{
	DN = new char[strlen(dn)+1];
	strcpy(DN,dn);



}


//folosit la compare...

LDAP_Operation::LDAP_Operation(char *dn,int auth,char *ldap_URI,char **optiuni,int nr_opt)
{
	DN = new char[strlen(dn)+1];
	strcpy(DN,dn);

	switch(auth)
	{
	case 0: this->auth_type = SIMPLE_AUTH;
		break;
	case 1: this->auth_type = SASL_AUTH;
		break;
	default:
		printf("\nAici va fi o exceptie....");
	}

	this->Ldap_URI = new char[strlen(ldap_URI)+1];
	strcpy(Ldap_URI,ldap_URI);


	this->nr_opt = nr_opt;

	this->option = new char*[this->nr_opt+1];
	for(int i=0;i<this->nr_opt;i++)
	{
		option[i]=new char[strlen(optiuni[i])+1];
		strcpy(option[i],optiuni[i]);
	}
}


//folosit la delete...

LDAP_Operation::LDAP_Operation(char *dn,int auth,char **optiuni,int nr_opt,vector<Atribut<char*>> atr)
{

	DN = new char[strlen(dn)+1];
	strcpy(DN,dn);

	switch(auth)
	{
	case 0: this->auth_type = SIMPLE_AUTH;
		break;
	case 1: this->auth_type = SASL_AUTH;
		break;
	default:
		printf("\nAici va fi o exceptie....");
	}


	this->nr_opt = nr_opt;

	this->option = new char*[this->nr_opt+1];
	for(int i=0;i<this->nr_opt;i++)
	{
		option[i]=new char[strlen(optiuni[i])+1];
		strcpy(option[i],optiuni[i]);
	}
	
	atribute = atr;
}



//folosita la add..


LDAP_Operation::LDAP_Operation(char *dn,int auth,char **opt,int nr)
{

	DN = new char[strlen(dn)+1];
	strcpy(DN,dn);

	this->nr_opt = nr;

	switch(auth)
	{
	case 0: this->auth_type = SIMPLE_AUTH;
		break;
	case 1: this->auth_type = SASL_AUTH;
		break;
	default:
		printf("\nAici va fi o exceptie....");
	}


	this->option = new char*[nr_opt+1];
	for(int i=0;i<nr_opt;i++)
	{
		option[i]=new char[strlen(opt[i])+1];
		strcpy(option[i],opt[i]);
	}


}


//..folosita la bind
LDAP_Operation::LDAP_Operation(char *dn,vector<Atribut<char*>> atribute)
{
	result=0;
	DN=new char[strlen(dn)+1];
	if(DN==0)
		throw new exception;
		//chiar daca va fi o problema cu alocarea dinamica
		//compilatorul isi va rezerva dinainte memorie pentru exceptii-->new
	strcpy(DN,dn);
	this->atribute=atribute;
	auth_type = SIMPLE_AUTH;

	this->nr_opt = 1;

	option = new char* [2];
	option[0]=new char[strlen("-LLL")+1];
	strcpy(option[0],"-LLL");

}

LDAP_Operation::LDAP_Operation(const LDAP_Operation& op)
{
	if(op.result!=0)
	{
		result=new char[strlen(op.result)+1];
		if(result==0)
			throw new exception;
		strcpy(result,op.result);
	}
	if(op.DN!=0)
	{
		DN=new char[strlen(op.DN)+1];
		if(DN==0)
			throw new exception;
		strcpy(DN,op.DN);
	}
	atribute=op.atribute;
}

LDAP_Operation& LDAP_Operation::operator=(const LDAP_Operation& op)
{
	if(this!=&op)
	{
		if(result!=0)
			delete[] result;
		if(DN!=0)
			delete[] DN;
		atribute.clear();
		if(op.result!=0)
		{
			result=new char[strlen(op.result)+1];
			if(result==0)
				throw new exception;
			strcpy(result,op.result);
		}
		if(op.DN!=0)
		{
			DN=new char[strlen(op.DN)+1];
			if(DN==0)
				throw new exception;
			strcpy(DN,op.DN);
		}
		atribute=op.atribute;
	}
	return (*this);
}

LDAP_Operation::~LDAP_Operation(void)
{
	if(DN!=0)
		delete[] DN;
	if(result!=0)
		delete[] result;
}

char* LDAP_Operation::daResult()const
{
	return result;
}

char* LDAP_Operation::getDN()const
{
	return DN;
}

vector<Atribut<char*>> LDAP_Operation::getAtribute()const
{
	return atribute;
}

void LDAP_Operation::addAtribut(Atribut<char*> atribut)
{
	atribute.push_back(atribut);
}

void LDAP_Operation::addAtribute(vector<Atribut<char*>> atribute)
{
	for(int i=0;i<atribute.size();i++)
		this->atribute.push_back(atribute[i]);
}





